package com.example.market3w;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * A simple {@link Fragment} subclass. Activities that contain this fragment
 * must implement the {@link MenuFragment.OnFragmentInteractionListener}
 * interface to handle interaction events. Use the
 * {@link MenuFragment#newInstance} factory method to create an instance of this
 * fragment.
 * 
 */
public class MenuFragment extends ListFragment{
	// TODO: Rename parameter arguments, choose names that match
	// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
	private static final String ARG_PARAM1 = "param1";
	private static final String ARG_PARAM2 = "param2";

	// TODO: Rename and change types of parameters
	private String mParam1;
	private String mParam2;
	private ListView menuElementsList ;
	private LinearLayout drawer ;
	private DrawerLayout menuLayout;
	private Fragment fragActuel;
	private View header;

	private OnFragmentInteractionListener mListener;

	/**
	 * Use this factory method to create a new instance of this fragment using
	 * the provided parameters.
	 * 
	 * @param param1
	 *            Parameter 1.
	 * @param param2
	 *            Parameter 2.
	 * @return A new instance of fragment MenuFragment.
	 */
	// TODO: Rename and change types and number of parameters
	public static MenuFragment newInstance(String param1, String param2) {
		MenuFragment fragment = new MenuFragment();
		Bundle args = new Bundle();
		args.putString(ARG_PARAM1, param1);
		args.putString(ARG_PARAM2, param2);
		fragment.setArguments(args);
		return fragment;
	}

	public MenuFragment() {
		// Required empty public constructor
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			mParam1 = getArguments().getString(ARG_PARAM1);
			mParam2 = getArguments().getString(ARG_PARAM2);
		}
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		View v =  inflater.inflate(R.layout.fragment_menu, container, false);

		return v;
	}

	@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
	
		menuElementsList = (ListView)getActivity().findViewById(android.R.id.list);
		drawer = (LinearLayout)getActivity().findViewById(R.id.drawer);
		menuLayout = (DrawerLayout) getActivity().findViewById(R.id.menu_layout);
		if(savedInstanceState == null){
			FragmentManager fm = getActivity().getSupportFragmentManager();
			FragmentTransaction ft = fm.beginTransaction();
			fragActuel = new HomeFragment();
			ft.add(R.id.fragment1, fragActuel);
			ft.commit();
			
		}
		header = View.inflate(getActivity().getBaseContext(), R.layout.title_item, null);
		
		remplirMenu();
		
	}
	// TODO: Rename method, update argument and hook method into UI event
	public void onButtonPressed(Uri uri) {
		if (mListener != null) {
			mListener.onFragmentInteraction(uri);
		}
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (OnFragmentInteractionListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnFragmentInteractionListener");
		}
	}

	@Override
	public void onDetach() {
		super.onDetach();
		mListener = null;
	}

	public void remplirMenu(){
		
		SharedPreferences preference = getActivity().getSharedPreferences(MainActivity.USER, 0);
		String login = preference.getString(MainActivity.USER_LOGIN, "null");
		String password = preference.getString(MainActivity.USER_PASSWORD, "null");
		ArrayList<HashMap<String, String>> listItem ;
		HashMap<String, String> contenu ;
		SimpleAdapter adapter;
		Resources res = getResources();
		listItem = new ArrayList<HashMap<String,String>>();
		
		contenu = new HashMap<String, String>();
		contenu.put("titre",res.getString(R.string.menu_accueil));
		contenu.put("description",res.getString(R.string.menu_accueil_description));
		listItem.add(contenu);
		
		contenu = new HashMap<String, String>();
		contenu.put("titre",res.getString(R.string.menu_telechargement));
		contenu.put("description",res.getString(R.string.menu_telechargement_description));
		listItem.add(contenu);
		
		contenu = new HashMap<String, String>();
		contenu.put("titre",res.getString(R.string.menu_video));
		contenu.put("description",res.getString(R.string.menu_video_description));
		listItem.add(contenu);
		
		contenu = new HashMap<String, String>();
		contenu.put("titre",res.getString(R.string.menu_rss));
		contenu.put("description",res.getString(R.string.menu_rss_description));
		listItem.add(contenu);
		
		contenu = new HashMap<String, String>();
		contenu.put("titre",res.getString(R.string.menu_geolocalisation));
		contenu.put("description",res.getString(R.string.menu_geolocalisation_description));
		listItem.add(contenu);
		
	    

	    TextView custom = (TextView)header.findViewById(R.id.Login);
	    if(login.equalsIgnoreCase("null")||password.equalsIgnoreCase("null")){
	    	custom.setText(res.getString(R.string.menu_connexion));
		    custom = (TextView)header.findViewById(R.id.mail);
		    custom.setText(res.getString(R.string.menu_inscription));
	    }else{
	    	custom.setText(login);
		    custom = (TextView)header.findViewById(R.id.mail);
		    custom.setText(password);
	    }
	    
	    
	    
	    
	    ImageView image = (ImageView)header.findViewById(R.id.imageTitre);
	    image.setImageResource(R.drawable.man);
	    
		if(menuElementsList.getHeaderViewsCount() == 0)menuElementsList.addHeaderView(header);
		adapter = new SimpleAdapter(getActivity().getBaseContext(), listItem, R.layout.element_menu, new String[]{"titre","description"}, new int[]{R.id.titre,R.id.description});
		
		menuElementsList.setAdapter(adapter);
		
		
		menuElementsList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,long id) {
				
				FragmentManager fm = getActivity().getSupportFragmentManager();
				FragmentTransaction ft = fm.beginTransaction();
				
				
			if(fragActuel != null)ft.remove(fragActuel);
			switch(position){
				case 0:
					if(fragActuel.getClass() == LoginRegisterFragment.class)break;
					fragActuel = new LoginRegisterFragment();
					ft.replace(R.id.fragment1,fragActuel );
					ft.commit();
					break;
				case 1:
					if(fragActuel.getClass() == HomeFragment.class)break;
					//fragActuel.onDestroyView();
					fragActuel = new HomeFragment();
					ft.replace(R.id.fragment1,fragActuel );
					ft.commit();
					break;
				
				case 2:
					if(fragActuel.getClass() == TelechargementFragment.class)break;
					//fragActuel.onDestroyView();
					fragActuel = new TelechargementFragment();
					ft.replace(R.id.fragment1,fragActuel );
					ft.commit();
					break;
				
				case 3:
					if(fragActuel.getClass() == VideoFragment.class)break;
					//fragActuel.onDestroyView();
					fragActuel = new VideoFragment();
					ft.replace(R.id.fragment1,fragActuel );
					ft.commit();
					break;
				case 4:
					if(fragActuel.getClass() == RSSFragment.class)break;
					//fragActuel.onDestroyView();
					fragActuel = new RSSFragment();
					ft.replace(R.id.fragment1,fragActuel );
					ft.commit();
					break;
				case 5:
					if(fragActuel.getClass() == GeolocalisationFragment.class)break;
					//fragActuel.onDestroyView();
					fragActuel = new GeolocalisationFragment();
					ft.replace(R.id.fragment1,fragActuel );
					ft.commit();
					break;
			}

			
			menuLayout.closeDrawer(drawer);
			}
		});
	
	}

}
